package com.fangs.yehwangglobal.base;

import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.viewbinding.ViewBinding;


import com.fangs.yehwangglobal.widget.eventbus.BindEventBus;
import com.fangs.yehwangglobal.widget.eventbus.EventManager;
import com.fangs.yehwangglobal.widget.loading.LoadingPopup;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;


public abstract class BaseFragment<VB extends ViewBinding> extends Fragment {

    protected VB viewBinding;
    //是否可见状态
    private boolean isVisible;
    //View已经初始化完成
    private boolean isPrepared;
    //是否第一次加载完
    private boolean isFirstLoad = true;
    private LoadingPopup loadingPopup;//网络请求时的背景透明不遮挡页面小进度提示  需要时显示

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
            EventManager.register(this);
        }
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Nullable
    @Override
    public View onCreateView(@NotNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        Class cls = (Class) type.getActualTypeArguments()[0];
        try {
            Method inflate = cls.getDeclaredMethod("inflate", LayoutInflater.class, ViewGroup.class, boolean.class);
            viewBinding = (VB) inflate.invoke(null, inflater, container, false);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        ViewGroup parent = (ViewGroup) viewBinding.getRoot().getParent();
        if (null != parent) {
            parent.removeView(viewBinding.getRoot());
        }
        isPrepared = true;
        return viewBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //初始化事件和获取数据, 在此方法中获取数据不是懒加载模式
        afterCreate(savedInstanceState);
        //在此方法中获取数据为懒加载模式
        lazyLoad();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        if (isVisibleToUser) {
            lazyLoad();
        }
    }

    protected abstract void afterCreate(Bundle savedInstanceState);

    protected abstract void lazyLoadData();

    protected void lazyLoad() {
        if (!isPrepared || !isVisible || !isFirstLoad) return;
        isFirstLoad = false;
        lazyLoadData();
    }

    public void showLoadingPopupAtLocation(View root) {
        if (loadingPopup == null) {
            loadingPopup = LoadingPopup.Companion.getInstance(requireContext());
        }
        loadingPopup.showNow(root);
    }

    public void dismissLoadingPopup() {
        if (loadingPopup != null) {
            loadingPopup.dismissNow();
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        viewBinding = null;
        if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
            EventManager.unregister(this);
        }
        dismissLoadingPopup();
    }

}
